app.controller("z-emailAddressSet-c", ['$scope', '$rootScope', '$z-http', '$sce', 'z-emailAddressSet-s', 'z-edit-menu-s','$filter',
    function ($scope, $rootScope, $zhttp, $sce, Service, MenuService, $filter) {
        $scope.viewModel = {};
        $scope.editModel = {};
        $scope.viewModel.emailAddress="";
        $scope.viewModel.emailPassword="";
        $scope.editModel.emailAddress="";
        $scope.viewModel.tokenCount=0;
        $scope.viewModel.time="";
        $scope.editModel.time="";
        
    	var now = new Date();                    //当前日期     
    	var nowMonth = now.getMonth();           //当前月     
    	var nowYear=$filter('date')(now,'yyyy'); //当前年
    	//获取当月开始日期
    	var monthStartDate = $filter("date")(new Date(nowYear, nowMonth, 1), "yyyy/MM/dd");
    	//获取当前日期
        var dayEndDate= $filter("date")(now, "yyyy/MM/dd");
    	$scope.viewModel.startDate=new Date(monthStartDate);    //起止日期
    	$scope.viewModel.date=new Date(monthStartDate);    //起止日期
//    	document.getElementById("crop").setAttribute("disabled", true);
    	
    	//日期控件
    	$scope.viewModel.dateOptions = {
    		formatYear: 'yy',
    		startingDay: 1,
    		showWeeks:false
    	};
    	//日期控件
    	$scope.viewModel.format = "yyyy/MM/dd";
    	//日期启动开关
    	$scope.viewModel.startDateOpened = false;
    	//起止日期选择
    	$scope.viewModel.startDateOpen = function($event) {
    		$event.preventDefault();
    		$event.stopPropagation();
    		$scope.viewModel.startDateOpened = !$scope.viewModel.startDateOpened;
    	};
    	
    	//日期启动开关
    	$scope.viewModel.dateOpened = false;
    	//起止日期选择
    	$scope.viewModel.dateOpen = function($event) {
    		$event.preventDefault();
    		$event.stopPropagation();
    		$scope.viewModel.dateOpened = !$scope.viewModel.dateOpened;
    	};
    	
        //查询页面名称
        MenuService.getMenuByUrl({url: 'emailAddressSet'},
            function (data) {
                $scope.viewModel.menuList = data;
            }, function (error) {
            }
        );
        
        $('#time').jHsDate({
			format:'hh:mm',
			callBack: function () {
				$scope.requestForm.time.$valid=true;
				$scope.requestForm.time.$dirty=true;
				$scope.requestForm.time.$invalid=false;
				$scope.editModel.time=$('#time').val();
			}	
		});

        //加载数据
        $scope.viewModel.bindData = function(){
        	$scope.viewModel.isLoading=true;
			//获取基本信息
			Service.getEmail(function(data){
				$scope.viewModel.emailAddress= data.mailName;
				$scope.viewModel.emailPassword= data.mailPassword;
    			$scope.viewModel.tokenCount=data.tokenCount;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$rootScope.alertWarning(error.fault.message);
				$scope.viewModel.isLoading=false;
			});
        };
        $scope.viewModel.bindData();

        //加载
        $scope.viewModel.load=function(){
            $scope.viewModel.show=true;
        };

        //关闭加载
        $scope.viewModel.unLoad=function(){
            $scope.viewModel.show=false;
            $scope.editModel.emailAddress="";
        };

        //修正
        $scope.viewModel.update=function(emailAddress){
            $scope.editModel.title="修改邮箱地址配置";
            $scope.editModel.data=angular.copy(emailAddress);
            $scope.viewModel.load();
        };
        
        //验证
        $scope.viewModel.emailForm= function() {
        	var int=0;
       	 	if(angular.isUndefined($scope.viewModel.emailAddress)){
	      		$scope.editForm.emailAddress.$dirty=true;
	      		int++;
	      	}

       	 	if($scope.viewModel.emailPassword=="" || angular.isUndefined($scope.viewModel.emailPassword)){
       	 		$scope.editForm.emailPassword.$dirty=true;
       	 		int++;
       	 	} 
       	 	
       	 	if(int>0){
       	 		return true;
       	 	}
       	 	return false;
        };
        
        //保存修改邮箱
        $scope.viewModel.save=function () {
        	if ($scope.viewModel.emailForm()) {
                return;
            }
        	$rootScope.alertConfirm("确定保存？",function() {
                $scope.viewModel.isLoading=true;
                Service.updateEmail({
                	emailAddress:$scope.viewModel.emailAddress,
        			emailPassword:$scope.viewModel.emailPassword
                },
        		function (data) {
        			$scope.viewModel.emailAddress=data.mailName;
        			$scope.viewModel.emailPassword=data.mailPassword;
        			$scope.viewModel.bindData();
                    $scope.viewModel.isLoading=false;
                    $rootScope.alertInfo("保存成功");
                    $scope.viewModel.unLoad();
                },
                function (error) {
                    $scope.viewModel.isLoading=false;
                    $rootScope.alertWarning(error.fault.message);
                });
            });
        };
        //生成
    	$scope.viewModel.saveAttendaceAuto=function(){
    		$rootScope.alertConfirm("确定重新读取考勤？",function() {
                $scope.viewModel.isLoading=true;
                Service.saveAttendaceAuto(
                    {thruDate:$scope.viewModel.startDate},
                    function(data){
                    	$rootScope.alertInfo("生成完成");
                        $scope.viewModel.isLoading = false;
                    },function(error) {
                        $scope.viewModel.isLoading = false;
                });
            });
    	};
    	
    	//班次生成
    	$scope.viewModel.saveSchedulDayKeyAuto=function(){
    		$rootScope.alertConfirm("确定重新生成班次？",function() {
                $scope.viewModel.isLoading=true;
                Service.saveSchedulDayKeyAuto(
                    function(data){
                    	$rootScope.alertInfo("生成完成");
                        $scope.viewModel.isLoading = false;
                    },function(error) {
                        $scope.viewModel.isLoading = false;
                });
            });
    	};
    	
    	//获取版本数据
    	/*$scope.viewModel.requestMaindataVersion=function(){
    		$rootScope.alertConfirm("确定获取版本数据吗？",function() {
                $scope.viewModel.isLoading=true;
                	Service.requestMaindataVersion({
                		version:$scope.viewModel.version
                	},
                	function(data){
                    	$rootScope.alertInfo("获取完成");
                        $scope.viewModel.isLoading = false;
                    },
                    function(error) {
                        $scope.viewModel.isLoading = false;
                        $rootScope.alertWarning(error.fault.message);
                });
            });
    	};*/
    	
    	//获取全量数据
    	$scope.viewModel.requestMaindata=function(){
    		$rootScope.alertConfirm("确定获取全量数据吗？",function() {
                $scope.viewModel.isLoading=true;
                $scope.viewModel.a==1
                	Service.requestMaindata(
                	function(data){
                    	$rootScope.alertInfo("获取完成");
                    	$scope.viewModel.bindData();
                        $scope.viewModel.isLoading = false;
                    },
                    function(error) {
                        $scope.viewModel.isLoading = false;
                        $rootScope.alertWarning(error.fault.message);
                });
            });
    	};
    	
    	  //格式化验证
        $scope.viewModel.escapeRegExp=function(string){
	        var pattern=/^([0-9]|[0-1][0-9]|2[0-3]):(2[0-9]|[0-5][0-9])$/;
	        var flag = pattern.test(string);
	        return flag;
        };
        
		//验证
		$scope.viewModel.requiredForm= function() { 
			if(!$scope.viewModel.escapeRegExp($scope.editModel.time)){
				$scope.requestForm.time.$dirty=true;
				return true;
			}
			return false;
        };
        
    	//获取新增数据
    	$scope.viewModel.requestMaindataNew=function(){
			if($scope.viewModel.requiredForm()){
				return;
			}
			var dateStr=$filter('date')($scope.viewModel.date,'yyyy-MM-dd') + " "+ $scope.editModel.time+":00";
    		$rootScope.alertConfirm("确定获取新增数据吗？",function() {
                $scope.viewModel.isLoading=true;
                $scope.viewModel.b=0;
                	Service.requestMaindataNew(
            			{thruDate:new Date(dateStr)},
                	function(data){
                    	$rootScope.alertInfo("获取完成");
                        $scope.viewModel.isLoading = false;
                    },
                    function(error) {
                        $scope.viewModel.isLoading = false;
                        $rootScope.alertWarning(error.fault.message);
                });
            	//设置请求增量数据按钮禁用
            	if($scope.viewModel.b==1){
            		var stamp2 = document.getElementById("stamp2");
            		stamp2.disabled=true;
            	}
            });
    	};
    }]
);